var userManagerForm_page={};

userManagerForm_page.action = {
    initBody:function () {
        userManagerForm_page.interface.initForm();
        userManagerForm_page.action.bindEvent();
    },
    bindEvent:function () {
        // 提交按钮的事件
        layui.use(['form'],function () {
            var form = layui.form;
            form.on('submit(saveBtn)', function (data) {
                userManagerForm_page.action.saveBtnClick();
                return false;
            });
        });
        // 取消按钮的事件
        $("#cancelBtn").click(function () {
            AppComm.utils.iframeCloseSelf();
            return false;
        });
    },
    // 在做action动作时校验数据data
    checkData:function (action, data) {
        var result = AppComm.data.newResult();

        if (data.groupId==""||data.groupId==null||data.groupId.trim()==""){
            result.code = Enums.ResultEnum().FAILURE.code;
            result.msg = "所属组织不能为空。";
            return result;
        }
        if (action=='addSubmit') {
            if (data.username==""||data.username==null||data.username.trim()==""){
                result.code = Enums.ResultEnum().FAILURE.code;
                result.msg = "账号不能为空。";
                return result;
            }
        }

        var flag = 0;
        var params={
            id:data.id,
            operationKey:data.operationKey,
        };

        var checkUrl = "";
        if (action=='editSubmit') {
            checkUrl = AppComm.url.getBaseURL()+'/userManager/ajax/getUserByUserNameOutCurrent';
        } else if (action=='addSubmit'){
            checkUrl = AppComm.url.getBaseURL()+'/userManager/ajax/getUserByUserName';
        }

        AppComm.ajax.execute({
            'url':checkUrl,
            'data':JSON.stringify(params),
            'async':false,
            'success':function (resultData) {
                if (resultData.code==Enums.ResultEnum().SUCCESS.code) {
                    if(resultData.data!=null){
                        flag++;
                    }
                }
            }
        });

        if(flag>0){
            result.code = Enums.ResultEnum().FAILURE.code;
            result.msg = "该账号已存在。";
        } else {
            result.code = Enums.ResultEnum().SUCCESS.code;
        }
        return result;
    },
    initData:function(rowId){
        userManagerForm_page.data.rowId = rowId;
    },
    saveBtnClick:function () {
        var groupIdValue = $("input[name='groupName_select_nodeId']").val();
        var groupNameValue = $('#groupName_select_input_id').val();
        if (groupIdValue==undefined || groupIdValue==null) {
            groupIdValue = $("#parentId").val();
        }

        var params = {
            id:$("#id").val(),
            pswd:$("#pswd").val(),
            username:$("#username").val(),
            fullname:$("#fullname").val(),
            phone:$("#phone").val(),
            email:$("#email").val(),
            comment:$("#comment").val(),
            orderId:$("#orderId").val(),
            groupId:groupIdValue,
            groupName:groupNameValue,
            theme:$("#theme").val(),
        };
        var result = userManagerForm_page.action.checkData(userManagerForm_page.data.action, params);
        if (result.code==Enums.ResultEnum().FAILURE.code) {
            AppComm.layer.warm(result.msg);
        } else {
            AppComm.ajax.post('/userManager/ajax/saveUser',params, function (resultData) {
                parent.AppComm.ajax.onSuccess(resultData);
                AppComm.utils.iframeCloseSelf();
                parent.userManager_page.action.initBody();
            });
        }
    }
};

userManagerForm_page.interface = {
    initForm:function () {
        if (userManagerForm_page.data.rowId == undefined || '' == userManagerForm_page.data.rowId.trim()) {
            // 添加
            var html = template("userManagerForm_template");
            $(".userManagerForm_box").html(html);
            userManagerForm_page.interface.initGroupDtree();
            userManagerForm_page.action.bindEvent();

        } else {
            // 编辑
            AppComm.ajax.post('/userManager/ajax/getUserById', {
                id: userManagerForm_page.data.rowId
            }, function (resultData) {
                var html = template("userManagerForm_template", resultData.data);
                $(".userManagerForm_box").html(html);
                userManagerForm_page.interface.initGroupDtree(resultData.data.user.groupId);
                userManagerForm_page.action.bindEvent();
            });
        }

    },
    initGroupDtree:function (defaultValue) {
        AppComm.ajax.post("/groupManager/ajax/getGroupList4Dtree", {}, function (resultData) {
            if (resultData.code==Enums.ResultEnum().SUCCESS.code) {
                layui.config({
                    base: '/static/comm/plugins/dtree/'
                }).use(['dtree','layer','jquery'],function(){
                    var dtree = layui.dtree, layer = layui.layer, $ = layui.jquery;
                    dtree.render({
                        elem: "#groupName",
                        width: "100%",
                        height: "50",
                        skin: "layui",
                        dataFormat: "list",
                        data: resultData.data,
                        selectTips: "请选择组织",
                        select: true, //指定下拉树模式
                        selectInitVal:defaultValue
                    });
                    userManagerForm_page.data.dtreeIns = dtree;
                });
            }
        });
    }
};

userManagerForm_page.data = {
    'rowId':undefined,
    'dtreeIns':undefined,
};